package com.woniuxy.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.admin.exception.ScoreException;
import com.woniuxy.admin.entity.AdminInfo;
import com.woniuxy.admin.entity.ScoreIncrease;
import com.woniuxy.admin.mapper.AdminInfoMapper;
import com.woniuxy.admin.mapper.ScoreIncreaseMapper;
import com.woniuxy.admin.service.ScoreIncreaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ...
 * @since 2024年05月22日
 */
@Service
public class ScoreIncreaseServiceImpl extends ServiceImpl<ScoreIncreaseMapper, ScoreIncrease> implements ScoreIncreaseService {

    @Resource
    private AdminInfoMapper adminInfoMapper;



    /**
     * 修改积分增加规则
     * @param increaseRatio 增加比例1元钱 增加多少积分
     * @param accountId 账户id
     */
    @Override
    public void updateScoreIncrease(BigDecimal increaseRatio, Integer accountId) {
        if(increaseRatio.compareTo(BigDecimal.ZERO)<0){
            throw new ScoreException(10001,"积分抵扣比例不能小于0");
        }
        List<ScoreIncrease> list = this.list();
        AdminInfo adminInfo = adminInfoMapper.selectOne(new QueryWrapper<AdminInfo>().eq("account_id", accountId));
        if(list==null){
            ScoreIncrease scoreIncrease = new ScoreIncrease();
            scoreIncrease.setIncreaseRatio(increaseRatio);
            scoreIncrease.setModifyBy(adminInfo.getRealName());
            scoreIncrease.setModifyTime(LocalDateTime.now());
            this.save(scoreIncrease);
        }else {
        ScoreIncrease scoreIncrease = list.get(0);
        scoreIncrease.setIncreaseRatio(increaseRatio);
        scoreIncrease.setModifyBy(adminInfo.getRealName());
        scoreIncrease.setModifyTime(LocalDateTime.now());
        this.updateById(scoreIncrease);
        }
    }
}
